Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list.
Constraints:
- The number of the nodes in the list is in the range
[0, 104]. -105 <= Node.val <= 105posis-1or a valid index in the linked-list.
Follow up: Can you solve it using O(1) (i.e. constant) memory?
Average Rating: 4.78 (259 votes)
Video Solution
Solution Article
This article is for beginners. It introduces the following ideas: Linked List, Hash Table and Two Pointers.
Approach 1: Hash Table
Intuition
To detect if a list is cyclic, we can check whether a node had been visited before. A natural way is to use a hash table.
Algorithm
We go through each node one by one and record each node's reference (or memory address) in a hash table. If the current node is null, we have reached the end of the list and it must not be cyclic. If current node’s reference is in the hash table, then return true.
Complexity analysis
Let n be the total number of nodes in the linked list.
-
Time complexity : O(n). We visit each of the n elements in the list at most once. Adding a node to the hash table costs only O(1) time.
-
Space complexity: O(n). The space depends on the number of elements added to the hash table, which contains at most n elements.
Approach 2: Floyd's Cycle Finding Algorithm
Intuition
Imagine two runners running on a track at different speed. What happens when the track is actually a circle?
Algorithm
The space complexity can be reduced to O(1) by considering two pointers at different speed - a slow pointer and a fast pointer. The slow pointer moves one step at a time while the fast pointer moves two steps at a time.
If there is no cycle in the list, the fast pointer will eventually reach the end and we can return false in this case.
Now consider a cyclic list and imagine the slow and fast pointers are two runners racing around a circle track. The fast runner will eventually meet the slow runner. Why? Consider this case (we name it case A) - The fast runner is just one step behind the slow runner. In the next iteration, they both increment one and two steps respectively and meet each other.
How about other cases? For example, we have not considered cases where the fast runner is two or three steps behind the slow runner yet. This is simple, because in the next or next's next iteration, this case will be reduced to case A mentioned above.
Complexity analysis
-
Time complexity : O(n). Let us denote n as the total number of nodes in the linked list. To analyze its time complexity, we consider the following two cases separately.
-
List has no cycle:
The fast pointer reaches the end first and the run time depends on the list's length, which is O(n). -
List has a cycle:
We break down the movement of the slow pointer into two steps, the non-cyclic part and the cyclic part:-
The slow pointer takes "non-cyclic length" steps to enter the cycle. At this point, the fast pointer has already reached the cycle. Number of iterations=non-cyclic length=N
-
Both pointers are now in the cycle. Consider two runners running in a cycle - the fast runner moves 2 steps while the slow runner moves 1 steps at a time. Since the speed difference is 1, it takes difference of speeddistance between the 2 runners loops for the fast runner to catch up with the slow runner. As the distance is at most "cyclic length K" and the speed difference is 1, we conclude that
Number of iterations=almost "cyclic length K".
-
Therefore, the worst case time complexity is O(N+K), which is O(n).
-
-
Space complexity : O(1). We only use two nodes (slow and fast) so the space complexity is O(1).
Last Edit: October 22, 2018 1:41 PM
The slow and fast points can also both start at head.
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
}
Last Edit: October 5, 2018 3:00 PM
I used a slightly faster destructive approach. I created a new node called "mark" and iterated through the list, setting the next value of each node to the mark node. When reaching the new node the first thing I did was check if the node is the mark node. If it ever is the mark node, we have looped.
March 10, 2019 1:50 AM
The last algorithm is called 'Floyd's Tortoise and Hare'
What is the role of pos here? I'm so confused! Because if this variable is given at input (which seems like it is), then it gives away the answer. So, why do we have it and what do we do with it? Thank you.
April 3, 2020 8:03 AM
Very confused because in the Ruby version there is no "pos" input as mentioned in the description - only a head node.
July 9, 2020 11:18 AM
What is role of pos in the problem description? Just to confuse the interview candidate?
Can someone please validate this answer. It passes the default test case, I don't know if its a fluke or not
I am new to leetcode
class Solution(object):
def hasCycle(self, head):
nodes = {}
while head != None:
if head in nodes:
return True
nodes[head] = head
head = head.next
return False
Last Edit: November 19, 2019 4:49 AM
Python3 code using two-pointers approach:
if not head or not head.next: return False
slow, fast = head, head.next
while slow != fast:
if not fast or not fast.next: return False
slow, fast = slow.next, fast.next.next
return True
March 13, 2019 2:22 AM
Solution: time complexity : O(n), space 0(1)
Reverse a linked list, if you get the same head that means
the linked List has a cycle otherwise it doesn't
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next:
return False
reverse_head = self.reverseList(head)
if reverse_head is head:
return True
else:
return False
def reverseList(self, head):
new_head = None
while head:
tmp = head.next
head.next = new_head
new_head = head
head = tmp
return new_head
Time Submitted | Status | Runtime | Memory | Language |
|---|---|---|---|---|
| 06/15/2021 09:06 | Accepted | 12 ms | 8 MB | cpp |
| 05/31/2021 19:23 | Accepted | 12 ms | 8.1 MB | cpp |
| 07/27/2020 08:27 | Accepted | 12 ms | 7.7 MB | cpp |
| 06/14/2020 15:51 | Wrong Answer | N/A | N/A | cpp |
| 06/11/2020 11:46 | Accepted | 7 ms | 43.2 MB | java |
xxxxxxxxxx/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while(fast != NULL && fast->next != NULL) { fast = fast->next->next; slow = slow->next; if(fast == slow) return true; } return false; }};